package com.lancoo.znbkxx.teacher.ui.fragment

import android.content.Intent
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lancoo.cpbase.authentication.base.CurrentUser
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.adapter.QuestionListAdapter
import com.lancoo.znbkxx.teacher.base.BaseVMFragment
import com.lancoo.znbkxx.teacher.base.SharedPreferencesHelper
import com.lancoo.znbkxx.teacher.databinding.ViewListBinding
import com.lancoo.znbkxx.teacher.model.OperationHelp
import com.lancoo.znbkxx.teacher.ui.activity.CommentDetailActivity
import com.lancoo.znbkxx.teacher.ui.activity.MainActivity
import com.lancoo.znbkxx.teacher.utils.LancooUtils
import com.lancoo.znbkxx.teacher.viewmodel.CommentViewModel

import kotlinx.android.synthetic.main.view_list.*

/**
 *@author Admin
 *@date 2021/1/22
 *
 */
class HardSolutionListFragment : BaseVMFragment<CommentViewModel, ViewListBinding>(),
        BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener {
    private lateinit var adapter: QuestionListAdapter
    private val type by lazy { arguments?.getInt("type") }
    override fun setLayoutID(): Int {
        return R.layout.view_list
    }

    override fun providerContext(): Any? {
        return activity
    }

    override fun providerVMClass(): Class<CommentViewModel> {
        return CommentViewModel::class.java
    }

    override fun initView() {
        val manager = LinearLayoutManager(
                requireContext()).apply { orientation = RecyclerView.VERTICAL }
        adapter = QuestionListAdapter(
                if (type == 0) viewModel.mHardSolution.value?.UnFinishedList else viewModel.mHardSolution.value?.FinishedList)
        val emptyView = LayoutInflater.from(context).inflate(R.layout.view_empty, null, false)
        adapter.emptyView = emptyView
        adapter.closeLoadAnimation()
        adapter.onItemClickListener = this
        adapter.onItemChildClickListener = this
        rv.layoutManager = manager
        rv.adapter = adapter
    }


    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        val dataList = (adapter as QuestionListAdapter).data
        startActivity(Intent(activity, CommentDetailActivity::class.java).apply {
            putExtra("QuesID", dataList[position].QuesID)
            putExtra("PaperID", dataList[position].PaperID)
            putExtra("SetID", dataList[position].SetID)
            putExtra("PaperName", dataList[position].PaperName)
            putExtra("isCommented", dataList[position].Finished)
        })
        if (dataList[position].Finished) {
            OperationHelp.operationLog(7, activity)
        }
    }


    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        val dataList = (adapter as QuestionListAdapter).data
        when (view?.id) {
            R.id.textView15 -> {
                startActivity(Intent(activity, CommentDetailActivity::class.java).apply {
                    putExtra("QuesID", dataList[position].QuesID)
                    putExtra("PaperID", dataList[position].PaperID)
                    putExtra("SetID", dataList[position].SetID)
                    putExtra("PaperName", dataList[position].PaperName)
                    putExtra("isCommented", dataList[position].Finished)
                })
                if (dataList[position].Finished) {
                    OperationHelp.operationLog(7, activity)
                }
            }
            R.id.textView16 -> {
                viewModel.getHardQuesPassedOrFailedStu(Ext.COURSE_CLASSID, Ext.KEY,
                        dataList[position].QuesID, CurrentUser.SchoolID, "C", CurrentUser.Token,
                        CurrentUser.UserID)
                OperationHelp.operationLog(8, activity)
            }
        }
    }

    override fun startObserve() {
        viewModel.mHardSolution.observe(this, Observer {
            val isAgreePermissions = SharedPreferencesHelper.getBoolean("isAgreePermissions", false)
            val list = it.TypeList.sortedByDescending { it.AvgScoreRate }
            if (list.isNotEmpty() && isAgreePermissions) {
                val data = if (type == 0) it.UnFinishedList.sortedByDescending { it.UpdateTime } else it.FinishedList.sortedByDescending { it.UpdateTime }
                for (finished in data) {
                    finished.Finished = type == 1
                }
                for (finished in data) {
                    if (QuestionListAdapter.isOutWeek(finished.UpdateTime)) {
                        finished.isShow = true
                        break
                    }
                }
                Handler().post { adapter.setNewData(data) }
            } else {
                Handler().post { adapter.setNewData(null) }
            }
        })
    }
}